package com.jianhua.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianhua.backend.domain.dto.CreateOrderDto;
import com.jianhua.backend.domain.dto.OrderDto;
import com.jianhua.backend.domain.entity.OrderItems;
import com.jianhua.backend.domain.entity.Orders;
import com.jianhua.backend.domain.vo.PageVo;
import com.jianhua.backend.mapper.OrdersMapper;
import com.jianhua.backend.request.OrderDetailResponse;
import com.jianhua.backend.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 存储用户订单的核心信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-02
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 创建订单（模拟）
     */
    @Override
    public String createOrder() {
        Orders order = new Orders();
        String orderId = UUID.randomUUID().toString(); // 生成唯一订单ID
        this.save(order);
        return orderId; // 返回订单ID
    }

    /**
     * 支付订单
     */
    @Override
    public boolean payOrder(String orderId) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getOrderId, orderId)
                .eq(Orders::getStatus, 0); // 只允许未支付订单支付

        Orders updateEntity = new Orders();

        int rowsAffected = ordersMapper.update(updateEntity, updateWrapper);
        return rowsAffected > 0;
    }

    /**
     * 获取订单详情
     */
    @Override
    public OrderDetailResponse getOrderDetail(String id) {
        Orders order = this.getOne(new LambdaQueryWrapper<Orders>().eq(Orders::getOrderId, id));
        if (order == null) {
            return null;
        }

        // 构建响应对象
        return new OrderDetailResponse(

        );

    }

    @Override
    public PageVo<Orders> listOrders(OrderDto orderDto) {
        Page<Orders> page = new Page<>(orderDto.getPage(), orderDto.getSize());
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        if(orderDto.getStatus() != null){
            wrapper.eq(Orders::getStatus, orderDto.getStatus());
        }
        Page<Orders> paged = page(page, wrapper);
        // 获取总记录数
        long total = page.getTotal();
        // 将总记录数转换为整型
        Integer count = Integer.valueOf((int) total);
        List<Orders> records = paged.getRecords();
        return new PageVo<>(count, records);
    }

    @Override
    public List<Orders> listOrders(Integer id) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, id);
        return ordersMapper.selectList(wrapper);
    }

    @Override
    public void createOrder(CreateOrderDto createOrderDto) {
        Orders orders = new Orders();
        orders.setUserId(createOrderDto.getUserId());
        orders.setDeliveryLocation(createOrderDto.getDeliveryLocation());
        orders.setTotalAmount(createOrderDto.getTotalAmount());
        ordersMapper.insert(orders);
    }
}
